package me.heyclock.hl.recordwriter.app;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;

import com.github.nukc.stateview.StateView;

import me.heyclock.hl.recordwriter.controller.BaseView;
import me.heyclock.hl.recordwriter.ui.RotateAnimatorProvider;

public class BaseFragment extends Fragment implements BaseView {
    /**
     * 进度条
     */
    private StateView stateView;
    private Context mContext;

    /**
     * des: 设置进度条+重试监听
     * author: hl
     * @param mContext
     * @param view
     * @param onRetryClickListener
     */
    protected void setStateView(Context mContext, View view, StateView.OnRetryClickListener onRetryClickListener){
        this.mContext = mContext;
        ///< 进度条
        stateView = StateView.inject(view);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
        ///< 设置重试事件
        stateView.setOnRetryClickListener(onRetryClickListener);
        //stateView.setLoadingResource(R.layout.base_loading); -- 重写base_loading.xml就可以替换默认主题
        //        final Animation circle_anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_round_rotate);
        //        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        //        circle_anim.setInterpolator(interpolator);
        //        stateView.setOnInflateListener(new StateView.OnInflateListener(){
        //            @Override
        //            public void onInflate(int viewType, View view) {
        //                if (viewType == StateView.LOADING) {
        //                    ViewGroup emptyView = (ViewGroup) view;
        //                    ImageView ivState = (ImageView) emptyView.findViewById(R.id.bl_stateview_iv);
        //                    if (circle_anim != null) {
        //                        ivState.startAnimation(circle_anim);  //开始动画
        //                    }
        //                }
        //            }
        //        } );
    }

    /**
     * des: 设置进度条
     * author: hl
     * @param mContext
     * @param view
     */
    protected void setStateView(Context mContext, View view){
        this.mContext = mContext;
        ///< 进度条
        stateView = StateView.inject(view);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
    }

    @Override
    public void showDialog() {
        stateView.showLoading();
    }

    @Override
    public void disDialog() {
        stateView.showContent();
    }

    @Override
    public void retryDialog() {
        stateView.showRetry();
    }

    @Override
    public void emptyDialog() {
        stateView.showEmpty();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
}
